﻿using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.FormPlugin.XHX
{
    [Description("资产卡片批量更新项目分类")]
    public class BatchProjectType : AbstractListPlugIn
    {
        public override void AfterBarItemClick(AfterBarItemClickEventArgs e)
        {
            base.AfterBarItemClick(e);
            if (e.BarItemKey.EqualsIgnoreCase("KCJT_tbProjectType"))
            {
                DynamicFormShowParameter showParam = new DynamicFormShowParameter();
                showParam.FormId = "KCJT_tbProjectType";
                this.View.ShowForm(showParam,
                    new Action<FormResult>((formResult) =>
                    {
                        if (formResult != null && formResult.ReturnData != null)
                        {
                            if (this.ListView.SelectedRowsInfo.Count > 0)
                            {
                                //string[] entryId = this.ListView.SelectedRowsInfo.GetEntryPrimaryKeyValues();
                                string[] entryId = this.ListView.SelectedRowsInfo.GetPrimaryKeyValues();
                                List<string> SqlArray = new List<string>();
                                int rows = 0;
                                for (int i = 0; i < entryId.Count(); i++)
                                {
                                    int id = int.Parse(entryId[i].ToString());
                                    string SqlStr = string.Format(@"/*dialect*/ update t1 set t1.F_KCJT_ASSISTANT = '" + ((S150804ReturnInfo)formResult.ReturnData).ProjectId + "' from t_fa_card t1 where t1.FASSETID={0}", id);
                                    SqlArray.Add(SqlStr);
                                    //int NOTE = DBServiceHelper.Execute(this.Context, SqlStr);
                                }
                                if (SqlArray.Count > 0)
                                {
                                    rows = DBServiceHelper.ExecuteBatch(this.Context, SqlArray);
                                }
                                if (rows > 0)
                                {
                                    this.View.ShowMessage("项目分类更新成功！");
                                }
                                else
                                {
                                    this.View.ShowMessage("没有可更新的数据，或者没有选中要更新的行！");
                                }
                            }

                        }
                    }));
            }
        }
    }

    internal class S150804ReturnInfo
    {
        public string ProjectId { get; set; }
    }
}
